package com.tungse.thread_join;

public class ThreadJoin {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Thread Start");

        TestThread t1 = new TestThread("ThreadA = ");
        TestThread t2 = new TestThread("ThreadB = ");

//       这样是单线程启动， 在t1执行完成后才会执行t2
//        t1.start();
//        t1.join();
//        t2.start();
//        t2.join();

//       这样是多线程启动，t1和t2会交替执行, 在全部执行完成后才会执行后续主线程的代码
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("Thread end");
    }


    static class TestThread extends Thread{
        String name;
        public TestThread(String name){
            this.name = name;
        }
        @Override
        public void run() {
            for(int i=0; i<5; i++){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(name + " = ." + i);
            }
        }
    }
}
